package com.ywm.educenter.controller;


import com.ywm.commonUtil.JwtUtils;
import com.ywm.commonUtil.R;
import com.ywm.commonUtil.orderVo.UcenterMemberOrder;
import com.ywm.educenter.entity.UcenterMember;
import com.ywm.educenter.entity.vo.RegisterVo;
import com.ywm.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 * @author maDondMei
 * @since 2020-05-25
 */
@RestController
@RequestMapping("/eduCenter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("/login")
    public  R loginUser(@RequestBody UcenterMember user) throws Exception {
        String token=memberService.login(user);
        return R.ok().data("token",token);
    }
    //注册
    @PostMapping("/register")
    public  R registerUser2(@RequestBody RegisterVo registerVo) throws Exception {
        memberService.register(registerVo);
        return R.ok();
    }
    //根据token获取登录信息
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类获取token中的id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(id);
        return R.ok().data("userInfo",member);
    }
    //根据id获取用户信息
    @PostMapping("/getUserInfoById/{id}")
    public UcenterMemberOrder getUserInfoById(@PathVariable String id){
        System.out.println("----------------*-*-*-*-*-*-*-*---------------");
        System.out.println(id);
        System.out.println("----------------*-*-*-*-*-*-*-*---------------");
        UcenterMember member = memberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder=new UcenterMemberOrder();
        //把member对象的值复制给UcenterMemberOrder
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //根据id获取用户信息
    @GetMapping("/getUserInfoById1/{id}")
    public UcenterMemberOrder getUserInfoById2(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder=new UcenterMemberOrder();
        //把member对象的值复制给UcenterMemberOrder
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //查询某一天的注册人数
    @GetMapping("/countRegister/{day}")
    public R countRegister(@PathVariable String day){
        int count=memberService.countRegister(day);
        return R.ok().data("countRegister",count);
    }
}

